package engine.control
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import newx.BaseObject;
	import newx.NxSprite;

	public class BaseControl extends BaseObject
	{
		/**组件容器*/
		protected 	var controlSpace	:NxSprite;
		
		protected 	var __skin			:MovieClip;
		
		/**组件的Id号*/
		private var _id				:int	=	0;
		
		/**是否可见*/		
		private var _visible			:Boolean	=	true;
		
		public function BaseControl(_skin:MovieClip)
		{
			__skin			=	_skin;
			controlSpace	=	new NxSprite();
		}
		
		/**
		 * 获取组件容器 
		 * @return 
		 * 
		 */		
		public function get content():NxSprite
		{
			return controlSpace;
		}
				
		public function get skin():MovieClip
		{
			return __skin;
		}
		
		public function set skin(_skin:MovieClip):void
		{
			if(_skin == null)
			{
				return;
			}
			__skin	=	_skin;
		}
		
		public function set x(_x:Number):void
		{
			controlSpace.x	=	_x;
		}
		
		public function get x():Number
		{
			return controlSpace.x;
		}
		
		public function set y(_y:Number):void
		{
			controlSpace.y	=	_y
		}
		public function get y():Number
		{
			return controlSpace.y;
		}
		
//		public  function  startDrag(_val:Boolean):void
//		{
//			controlSpace.startDrag(_val,);
//		}
//		
//		public function stopDrag():void
//		{
//			controlSpace.stopDrag();
//		}
		public function set depth(val:uint):void
		{
			controlSpace.parent.setChildIndex(controlSpace,val);
		}
		public function get depth():uint
		{
			var index:uint	=	controlSpace.parent.getChildIndex(controlSpace);
			return index;
		}
		/**
		 *设定组件的id号 
		 */		
		public function set id(val:int):void
		{
			_id	=	val;
		}
		public function get id():int
		{
			return _id
		}
		
		public function set visible(val:Boolean):void
		{
			_visible	=	val
			controlSpace.visible	=	val;
		}
		public function get visible():Boolean
		{
			return _visible;
		}
		
//		public function get parentView():DisplayObjectContainer
//		{
//			return __parent;
//		}
//		
//		public function set parentView(val:DisplayObjectContainer):void
//		{
//			__parent	=	val;
//		}
		
		public function show(space:DisplayObjectContainer	= null,addedCallBack:Function = null):void
		{			
			if(controlSpace.stage == null)
			{
				if(addedCallBack!=null)
				{
					HandleProperties("addedCallBack",addedCallBack);
					controlSpace.addEventListener(Event.ADDED_TO_STAGE,onSelfBeAdd);
				}		
				if(null != space)
				{
					space.addChild(controlSpace);
				}
			}
			else
			{
				controlSpace.visible	=	true;
				
				if(addedCallBack!=null)
				{
					addedCallBack.call(this);
				}				
			}			
		}
		
		protected function onSelfBeAdd(e:Event):void
		{
			var tFun:Function	=	GetProperties("addedCallBack");
			tFun.call(this);
			tFun				=	null;
			DeleteProperties("addedCallBack");
			controlSpace.removeEventListener(Event.ADDED_TO_STAGE,onSelfBeAdd);
		}
		
		/**
		 *隐藏组件 
		 */	
		public function hide():void
		{
			controlSpace.visible	=	false;
		}
		
		public function getTrueRect(val:DisplayObjectContainer = null):Rectangle
		{
			if(val == null)
			{
				val	=	controlSpace;
			}
			var rect		:Rectangle	=	new Rectangle(0, 0, 0, 0);
			var child		:DisplayObject;
			var tSize		:uint		=	val.numChildren;
			for(var i:int = 0; i < tSize; i ++)
			{
				child					=	val.getChildAt(i);
				rect					=	rect.union(child.getBounds(val));
			}
			return rect;
		}
	}
}